package club.weiyuanstudio.android.app.message_station.broadcast.message

import android.telephony.SmsMessage
import club.weiyuanstudio.android.app.message_station.api.data.Message
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.time.format.DateTimeFormatter
import java.util.*

data class SimpleMessage(
    val originatingAddress: String,
    val messageBody: String,
    val receiveTime: Date
) {
    /**
     * create simple message by pdu
     */
    constructor(pdu: ByteArray, format: String) : this(
        SmsMessage.createFromPdu(pdu, format)
    )

    constructor(smsMessage: SmsMessage) : this(
        smsMessage.displayOriginatingAddress,
        smsMessage.displayMessageBody,
    )

    constructor(originatingAddress: String, messageBody: String) : this(
        originatingAddress,
        messageBody,
        Date()
    )

    override fun toString(): String {
        return "SimpleMessage(originatingAddress='$originatingAddress', messageBody='$messageBody', receiveTime=$receiveTime)"
    }

    fun getMessage(): Message {
        val fmtDateTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).format(receiveTime)
        return Message(originatingAddress, messageBody, fmtDateTime)
    }
}
